package service

import (
	"anban/models"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取作业通知信息
func GetHomeworkInfo(p map[string]interface{}) *models.Homework {
	o := orm.NewOrm()
	homework := &models.Homework{}
	qs := o.QueryTable("Homework")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["created"]; ok {
		qs = qs.Filter("created", p["created"])
	}
	qs.One(homework)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if homework.Id > 0 {
			homework.CreatedShow = homework.Created.Format("2006-01-02 15:04:05")
		}
	}
	return homework
}

// 添加作业通知
func AddHomework(input url.Values) (int64, error) {
	o := orm.NewOrm()
	homework := &models.Homework{}
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	homework.User = user
	class := &models.Class{}
	if _, ok := input["class_id"]; ok {
		class.Id = utils.Atoi64(input["class_id"][0])
		o.Read(class)
	}
	homework.Class = class
	course := &models.Course{}
	if _, ok := input["course_id"]; ok {
		course.Id = utils.Atoi64(input["course_id"][0])
		o.Read(course)
	}
	homework.Course = course
	if _, ok := input["content"]; ok {
		homework.Content = input["content"][0]
	}
	return o.Insert(homework)
}

// 修改作业通知信息
func EditHomework(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("Homework").Filter("id", input["id"][0]).Update(p)
}

// 获取作业通知列表
func GetHomeworkList(p map[string]interface{}) (int64, []*models.Homework) {
	var homeworkList []*models.Homework
	o := orm.NewOrm()
	qs := o.QueryTable("Homework")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["classId"]; ok {
		qs = qs.Filter("class_id", p["classId"])
	}
	if _, ok := p["schoolId"]; ok {
		qs = qs.Filter("Class__School__Id", p["schoolId"])
	}
	if _, ok := p["classIds"]; ok && len(p["classIds"].([]int64)) > 0 {
		qs = qs.Filter("class_id__in", p["classIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&homeworkList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, homework := range homeworkList {
			homework.CreatedShow = homework.Created.Format("2006-01-02 15:04:05")
			homework.CreatedDateShow = homework.Created.Format("2006-01-02")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, homework := range homeworkList {
			homework.TypeName = fmt.Sprintf("%s作业", homework.Course.Name)
			homework.ClassName = homework.Class.Name
			homework.CreatedDateShow = homework.Created.Format("2006-1-2")
		}
	}
	return totalCount, homeworkList
}
